package cn.zwxky.android.staff.My;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.widget.ScrollView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.ListViewForScrollView;
import cn.zwxky.android.staff.Utils.LittleUtils;
import cn.zwxky.android.staff.Utils.URLString;
import cn.zwxky.android.staff.Utils.WebActivity;

/**
 * 公司公告页面
 * Created by Administrator on 2017/3/29 0029.
 */
@ContentView(R.layout.aty_company_msg)
public class CompanyMsgActivity extends LsfbActivity {

    private boolean isload = false;

    /**
     * list列表
     */
    @ViewInject(R.id.fg_news_list)
    private ListViewForScrollView news_list;

    @ViewInject(R.id.refershlayout)
    private PullToRefreshScrollView refreshScrollView;

    /**
     * RecyclerView的布局管理器
     */
    private RecyclerView.LayoutManager mlayoutManager;

    private List<CompanyMsgBean.ListBean> mList;

    private CompanyMsgAdapter adapter;
    private int page = 0;


    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "公司公告");

        mList = new ArrayList<>();
        adapter = new CompanyMsgAdapter(this, R.layout.item_company_msg, mList);
        news_list.setAdapter(adapter);

        refreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
        refreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                isload = true;
                page = 0;
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page++;
                isload = false;
                getData();
            }
        });
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        isload = true;
        page = 0;
        getData();
    }

    public void getData(){
        HashMap<String, String> hashMap = new HashMap<String, String>();
          hashMap.put("uid", Config.getUserId());
          hashMap.put("num", "10");
          hashMap.put("page", String.valueOf(page));
        new BaseInternet().getData_Get(URLString.NEWSLIST, hashMap, CompanyMsgBean.class, new CompanyMsgEvent<CompanyMsgBean>(), true,true);
    }

    @EventAnnotation
    public void CompanyMsgEvent(CompanyMsgEvent<CompanyMsgBean> event){
        refreshScrollView.onRefreshComplete();
        switch (event.getCode()) {
            case 200:
                if (isload) {
                    mList.clear();
                }
                mList.addAll(event.getData().getList());
                adapter.notifyDataSetChanged();
                break;
            default:
                if (isload) {
                    mList.clear();
                    SN.SnackShow(news_list,event.getMsg());
                } else {
                    page--;
                    SN.SnackShow(news_list,"没有更多数据");
                }
                break;
        }
    }

    @EventAnnotation
    public void NewsClickEvent(NewsClickEvent event){
        startActivity(new Intent(this, WebActivity.class).putExtra("url", URLString.WED_URL+event.getData()).putExtra("title","查看全文"));
    }

}
